package com.flyan.game.motafx.effect.npc;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.entity.Entity;
import com.flyan.game.motafx.core.panel.GameInfoPanel;
import com.flyan.game.motafx.core.util.cutscene.CutsceneUtils;
import com.flyan.game.motafx.core.util.entity.EntityUtils;
import com.flyan.game.motafx.effect.BaseEffect;
import com.flyan.game.motafx.entity.npc.NpcEntityInfo;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

/**
 * 聊天过场动画效果
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/8
 */
public class ChatCutsceneEffect extends BaseEffect {

    public ChatCutsceneEffect() {
        super(Duration.seconds(0.1));
    }

    @Override
    public void onStart(@NotNull Entity entity) {
        NpcEntityInfo npcInfo = EntityUtils.getEntityInfo(triggerEntity, NpcEntityInfo.class);
        if (StrUtil.isNotEmpty(npcInfo.getCutsceneScript())) {
            CutsceneUtils.startCutsceneByScript(npcInfo.getCutsceneScript(), () -> {
                /* 过场动画完成后回调效果 */
                if (StrUtil.isNotEmpty(npcInfo.getCutsceneOnFinishCallbackEffect())) {
                    BaseEffect effect = ReflectUtil.newInstance(npcInfo.getCutsceneOnFinishCallbackEffect());
                    effect.setTriggerEntity(triggerEntity);
                    entity.getComponent(EffectComponent.class).startEffect(effect);
                }
            });
        }
        GameInfoPanel.redraw();
    }

    @Override
    public void onEnd(@NotNull Entity entity) {

    }
}
